﻿#ifndef C3FDRENDERWIDGET_H
#define C3FDRENDERWIDGET_H

#include <QVTKOpenGLNativeWidget.h>
#include <QFocusEvent>
#include <QWidget>
#include <QList>
#include <QString>
#include <vtkDataSet.h>
#include <vtkStructuredGrid.h>
#include <vtkDataSetMapper.h>
#include <vtkActor.h>
#include <vtkTextWidget.h>
#include <vtkOrientationMarkerWidget.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkScalarBarWidget.h>
#include <vtkCellDataToPointData.h>
#include <QMap>
#include "Common/Types.h"

namespace MainWidget
{
	class VtkRenderWidget : public QVTKOpenGLNativeWidget {
		Q_OBJECT
	public:
		VtkRenderWidget(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
		~VtkRenderWidget() override = default;
	signals:
		void showBorderSig(bool isShow);
	protected:
		void focusInEvent(QFocusEvent *event) override;
		void focusOutEvent(QFocusEvent *event) override;
	};

	class C3fdRenderWidget : public QWidget
	{
		Q_OBJECT
	public:
		C3fdRenderWidget(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
		~C3fdRenderWidget() override;

		void updateShadeField(QString field);
		void addStructuredGrid(QList<vtkSmartPointer<vtkStructuredGrid>>, QColor, Common::DisplayMode mode = Common::DisplayMode::Surface);
		void addStructuredGrid(vtkSmartPointer<vtkStructuredGrid>, QColor, Common::DisplayMode mode = Common::DisplayMode::Surface);
		void addPolyData(vtkPolyData*, QColor);
		void setBackGroundColor(QColor color1, QColor color2);
		void setBackGroundColor(QColor color);
		void setDisplayMode(Common::DisplayMode mode);
		void addAxes();
		QString getTitle();
		void setTitle(QString title);
		void addTitle(QString title);
		void setView(QString view);
		void fitView();
		void setViewXPlus();
		void setViewXMiuns();
		void setViewYPlus();
		void setViewYMiuns();
		void setViewZPlus();
		void setViewZMiuns();
		void refreshRender();

		void enableFocusSignal(bool enable);

		vtkSmartPointer<vtkRenderer> getRenderer() const;
		vtkSmartPointer<vtkRenderWindow> getRenderWindow() const;

		void clearAllActor();

		QMap<Common::FieldLocation, QList<QString>> getFields();
		void saveImage(QString filePath, int width = 0, int height = 0, Common::AspectRatioMode mode = Common::AspectRatioMode::Undefined, int quality = 100);
		QString getShadeField() const;

		bool saveVtkFile(QString filename);

	signals:
		void focusInSig(QWidget *, QString type = "vtk");

	public slots:
		void showBorderSlot(bool isShow);

	protected:
		void focusInEvent(QFocusEvent *event) override;
		void focusOutEvent(QFocusEvent *event) override;
		void paintEvent(QPaintEvent* event) override;

	private:
		VtkRenderWidget* m_VtkWidget = nullptr;
		QMap<Common::FieldLocation, QList<QString>> m_Fields = {};

		QColor m_Color1, m_Color2;
		vtkSmartPointer<vtkRenderWindow> m_RenderWindow = nullptr;
		vtkSmartPointer<vtkRenderer> m_Renderer = nullptr;
		QList<vtkSmartPointer<vtkStructuredGrid>> m_Grids = {};
		QList<vtkSmartPointer<vtkCellDataToPointData>> m_CellToPointGrids = {};

		QList<vtkSmartPointer<vtkDataSetMapper>> m_Mappers = {};
		QList<vtkSmartPointer<vtkActor>> m_Actors = {};

		vtkSmartPointer<vtkScalarBarActor> m_ScalarBarActor = nullptr;
		vtkSmartPointer<vtkScalarBarWidget> m_ScalarBarWidget = nullptr;
		vtkSmartPointer<vtkTextWidget> m_TitleWidget = nullptr;
		vtkSmartPointer<vtkOrientationMarkerWidget> m_AxesWidget = nullptr;

		bool m_EnableFocusSignal = false;
		bool m_IsFocus = false;
		QString m_Title = "test";
		QString m_CurrentShadeField = "";

	};
} // namespace name

#endif